package com.link.play.fit.ai.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;

import androidx.annotation.IntDef;
import androidx.appcompat.widget.AppCompatTextView;

import com.link.play.fit.ai.R;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class CircleStatusTextView extends AppCompatTextView {
    private static final int DEFAULT_STROKE_WIDTH = 1; // 默认描边宽度
    private static final int DEFAULT_RED_COLOR = Color.RED;
    private static final int DEFAULT_GREEN_COLOR = Color.GREEN;

    private Paint mCirclePaint;
    private int mStrokeWidth;
    private int mRedColor;
    private int mGreenColor;
    private int mCurrentStatus = 0;

    // 进度相关
    private float mCurrentValue = 0;
    private float mMaxValue = 100;

    public CircleStatusTextView(Context context) {
        super(context);
        init(context, null);
    }

    public CircleStatusTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CircleStatusTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        // 初始化默认值
        mStrokeWidth = dp2px(DEFAULT_STROKE_WIDTH);
        mRedColor = DEFAULT_RED_COLOR;
        mGreenColor = DEFAULT_GREEN_COLOR;

        // 读取自定义属性
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleStatusTextView);
            mStrokeWidth = a.getDimensionPixelSize(
                    R.styleable.CircleStatusTextView_circle_stroke_width,
                    mStrokeWidth
            );
            mRedColor = a.getColor(
                    R.styleable.CircleStatusTextView_red_color,
                    DEFAULT_RED_COLOR
            );
            mGreenColor = a.getColor(
                    R.styleable.CircleStatusTextView_green_color,
                    DEFAULT_GREEN_COLOR
            );
            mCurrentStatus = a.getInt(R.styleable.CircleStatusTextView_show_circle, 0);
            a.recycle();
        }

        // 初始化画笔
        mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCirclePaint.setStyle(Paint.Style.STROKE);
        mCirclePaint.setStrokeWidth(mStrokeWidth);
        mCirclePaint.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (mCurrentStatus == 0) return; // 不显示圆圈

        // 计算绘制区域
        int padding = mStrokeWidth / 2;
        RectF rect = new RectF(
                padding,
                padding,
                getWidth() - padding,
                getHeight() - padding
        );

        switch (mCurrentStatus) {
            case 1: // 红色圆弧
                mCirclePaint.setColor(mRedColor);
                float sweepAngle = (mCurrentValue / mMaxValue) * 360;
                canvas.drawArc(rect, -90, sweepAngle, false, mCirclePaint);
                break;

            case 2: // 绿色圆圈
                mCirclePaint.setColor(mGreenColor);
                canvas.drawCircle(
                        getWidth() / 2f,
                        getHeight() / 2f,
                        (Math.min(getWidth(), getHeight()) - mStrokeWidth) / 2f,
                        mCirclePaint
                );
                break;
        }
    }

    // 设置当前状态
    public void setStatus(@Status int status) {
        mCurrentStatus = status;
        invalidate();
    }

    // 设置进度值
    public void setProgress(float value, float max) {
        mCurrentValue = value;
        mMaxValue = max;
        if (mCurrentStatus == 1) invalidate();
    }

    // dp转px
    private int dp2px(float dp) {
        return (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                dp,
                getResources().getDisplayMetrics()
        );
    }

    // 状态枚举
    @IntDef({Status.NONE, Status.RED_ARC, Status.GREEN_CIRCLE})
    @Retention(RetentionPolicy.SOURCE)
    public @interface Status {
        int NONE = 0;
        int RED_ARC = 1;
        int GREEN_CIRCLE = 2;
    }
}